/*
 * ItemCellRenderer.java
 *
 * Created on August 8, 2006, 9:27 PM
 */

package com.thornapple.ebay.manager.ui;

import com.ebay.soap.eBLBaseComponents.PictureDetailsType;
import com.elevenworks.swing.panel.SimpleGradientPanel;
import com.elevenworks.swing.panel.TigerInfoPanelUI;
import com.sun.javaone.aerith.g2d.AnimationUtil;
import com.thornapple.ebay.manager.AuctionItem;
import com.thornapple.ebay.manager.adapter.EbayItemAdapter;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.TimingTarget;
import org.jdesktop.jdic.browser.BrowserEngineManager;
import org.jdesktop.jdic.browser.WebBrowser;
import org.jdesktop.swingx.JXImagePanel;
import org.jdesktop.swingx.painter.gradient.BasicGradientPainter;

/**
 *
 * @author  Bill
 */
public class ItemDetailPanel extends SimpleGradientPanel {
    
    private static final BufferedImage NO_IMAGE =
            new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
    
    public static Image LOADING;
    static {
        try {
            LOADING = ImageIO.read(ItemDetailPanel.class.getResource("/resources/photos/loading.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private WebBrowser browser;
    
    private EbayItemAdapter adapter = new EbayItemAdapter();
    private AuctionItem item;
    
    /** Creates new form ItemCellRenderer */
    public ItemDetailPanel() {
        initComponents();
        imageList.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e) {
                jXImagePanel1.setImage((Image)imageList.getModel().getElementAt(imageList.getSelectedIndex()));
                jXImagePanel1.repaint();
            }});
    }
    
    /** Creates new form ItemCellRenderer */
    public ItemDetailPanel(AuctionItem item) {
        initComponents();
        this.item = item;
         imageList.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e) {
                jXImagePanel1.setImage((Image)imageList.getModel().getElementAt(e.getFirstIndex()));
            }});
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        jXPanel2.setUI(new TigerInfoPanelUI());
        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jXImagePanel1 = new org.jdesktop.swingx.JXImagePanel();
        jXImagePanel1.setStyle(JXImagePanel.Style.SCALED);
        jXImagePanel1.setUI(new TigerInfoPanelUI());
        imageListScrollPane = new javax.swing.JScrollPane();
        imageListScrollPane.getViewport().setOpaque(false);
        imageList = new javax.swing.JList();
        imageList.setOpaque(false);
        imageList.setCellRenderer(new PhotoListCellRenderer());
        browserPanel = new javax.swing.JPanel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXPanel1.setBackgroundPainter(new BasicGradientPainter(BasicGradientPainter.AERITH));
        lblPrice = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblEndTime = new javax.swing.JLabel();
        lblBids = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtTitle = new javax.swing.JTextArea();

        jButton1.setText("Pictures");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Description");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jXPanel2.setLayout(new java.awt.CardLayout());

        jXPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        org.jdesktop.layout.GroupLayout jXImagePanel1Layout = new org.jdesktop.layout.GroupLayout(jXImagePanel1);
        jXImagePanel1.setLayout(jXImagePanel1Layout);
        jXImagePanel1Layout.setHorizontalGroup(
            jXImagePanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 409, Short.MAX_VALUE)
        );
        jXImagePanel1Layout.setVerticalGroup(
            jXImagePanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 278, Short.MAX_VALUE)
        );

        imageListScrollPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 6, 1));
        imageListScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        imageListScrollPane.setOpaque(false);
        imageList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        imageList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
        imageList.setMaximumSize(new java.awt.Dimension(32767, 32767));
        imageList.setMinimumSize(new java.awt.Dimension(81, 56));
        imageList.setOpaque(false);
        imageList.setVisibleRowCount(1);
        imageListScrollPane.setViewportView(imageList);

        org.jdesktop.layout.GroupLayout jXPanel3Layout = new org.jdesktop.layout.GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(
            jXPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jXPanel3Layout.createSequentialGroup()
                .add(1, 1, 1)
                .add(imageListScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE))
            .add(jXImagePanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jXPanel3Layout.setVerticalGroup(
            jXPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jXPanel3Layout.createSequentialGroup()
                .add(jXImagePanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(imageListScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jXPanel2.add(jXPanel3, "card2");

        browserPanel.setLayout(new java.awt.BorderLayout());

        browserPanel.setMaximumSize(new java.awt.Dimension(300, 2147483647));
        jXPanel2.add(browserPanel, "card3");

        jXPanel1.setAlpha(0.4F);
        jXPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblPrice.setFont(new java.awt.Font("Tahoma", 3, 18));
        lblPrice.setText("Price");

        jLabel1.setText("End Time:");

        jLabel2.setText("Bids:");

        lblEndTime.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblEndTime.setText("jLabel3");

        lblBids.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblBids.setText("jLabel4");

        jScrollPane1.setBorder(null);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        txtTitle.setBackground(new java.awt.Color(235, 233, 237));
        txtTitle.setColumns(20);
        txtTitle.setEditable(false);
        txtTitle.setFont(new java.awt.Font("Tahoma", 1, 12));
        txtTitle.setLineWrap(true);
        txtTitle.setRows(5);
        jScrollPane1.setViewportView(txtTitle);

        org.jdesktop.layout.GroupLayout jXPanel1Layout = new org.jdesktop.layout.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jXPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                    .add(jXPanel1Layout.createSequentialGroup()
                        .add(lblPrice)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 263, Short.MAX_VALUE)
                        .add(jXPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jXPanel1Layout.createSequentialGroup()
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblEndTime))
                            .add(jXPanel1Layout.createSequentialGroup()
                                .add(jLabel2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lblBids)))))
                .addContainerGap())
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jXPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblPrice)
                    .add(jXPanel1Layout.createSequentialGroup()
                        .add(jXPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblEndTime)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jXPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(lblBids))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jXPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jXPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jXPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(7, 7, 7)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jXPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        CardLayout layout = (CardLayout) jXPanel2.getLayout();
        layout.first(jXPanel2);
        
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        CardLayout layout = (CardLayout) jXPanel2.getLayout();
        layout.last(jXPanel2);
    }//GEN-LAST:event_jButton2ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel browserPanel;
    private javax.swing.JList imageList;
    private javax.swing.JScrollPane imageListScrollPane;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXImagePanel jXImagePanel1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private javax.swing.JLabel lblBids;
    private javax.swing.JLabel lblEndTime;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JTextArea txtTitle;
    // End of variables declaration//GEN-END:variables
    
    public AuctionItem getItem() {
        return item;
    }
    //TODO should be on a separate thread
    public void setItem(AuctionItem item) {
        if (browser == null){ //cant have this called as part of bean instantiation
            BrowserEngineManager.instance().setActiveEngine(BrowserEngineManager.IE);
            browser = new WebBrowser();
            browserPanel.add(browser,BorderLayout.CENTER);
        }
        
        this.item = item;
        try {
            item = adapter.getItemDetails(item.getItem().getItemID().getValue());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        this.txtTitle.setText(item.getItem().getTitle());
        this.lblPrice.setText("$"+item.getCurrentPrice());
        this.browser.setContent(item.getItem().getDescription());
        lblBids.setText(item.getBids());
        lblEndTime.setText(item.getTimeLeft());
        
        PictureDetailsType pics = item.getItem().getPictureDetails();
        if (pics != null){
            /*if (pics.getGalleryURL() != null){
                try {
                    URI location = new URI(pics.getGalleryURL().toString());
                    System.out.println(location);
                    jXImagePanel1.setImage(ImageIO.read(location.toURL()));
                }catch(Exception e){
                }
            } else*/ if (pics.getPictureURL() != null && pics.getPictureURL().length > 0){
                try {
                    imageList.setModel(new ImageListModel(pics));
                    URI location = new URI(pics.getPictureURL(0).toString());
                    //System.out.println(location);
                    jXImagePanel1.setImage(ImageIO.read(location.toURL()));
                }catch(Exception e){
                }
            } else {
                System.out.println("No item available.");
                jXImagePanel1.setImage(NO_IMAGE);
            }
        }
        
    }
    
    public void transitionIn(){
        final Container parent = jXPanel2.getParent();
        Animator fadeOut = AnimationUtil.createFadeInAnimation(jXPanel2);
        fadeOut.addTarget(new TimingTarget() {
            public void begin() {parent.repaint();}
            public void end() {
                //parent.remove(ItemDetailPanel.this);
                parent.repaint();
            }
            public void repeat(){}
            public void timingEvent(float f) {parent.repaint();}
        });
        fadeOut.start();
    }
    
    public void transitionOut(){
        final Container parent = jXPanel2.getParent();
        Animator fadeOut = AnimationUtil.createFadeOutAnimation(jXPanel2);
        fadeOut.addTarget(new TimingTarget() {
            public void begin() {parent.repaint();}
            public void end() {
                //parent.remove(ItemDetailPanel.this);
                parent.repaint();
            }
            public void repeat(){}
            public void timingEvent(float f) {parent.repaint();}
        });
        fadeOut.start();
    }
    
    
    private class ImageListModel extends AbstractListModel {
        private PictureDetailsType pics;
        private List imageList;
        
        public ImageListModel(PictureDetailsType pics) {
            this.pics = pics;
            imageList = new ArrayList(getSize());
            Thread loader = new Thread(){public void run(){
                try {loadImagesIntoModel();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }}};
            loader.start();    
        }
        
        public Object getElementAt(int index) {
            if (index >= imageList.size()) return null;
            return imageList.get(index);
        }
        
        public int getSize() {
            return pics.getPictureURL() == null ? 0 : pics.getPictureURL().length;
        }
        
        public void fireContentsChanged() {
            fireContentsChanged(this, 0, getSize());
        }
        
        private void loadImagesIntoModel() throws Exception {
            int size = getSize();
            for (int i = 0; i < size; i++) {
                imageList.add(ImageIO.read(new URI(pics.getPictureURL()[i].toString()).toURL()));
                //System.out.println("loading thumbnail from " + new URI(value.toString()).toURL());)
                fireContentsChanged();
            }
        }
    }
}
